Skip to content

AvgTimeToFirstToken metric

Bases: LMOperationalMetric

Mean time to first token of streamed calls; for UX latency, 0 unstreamed.

Measured per streamed call as the wall-clock from the provider request start to the first non-empty chunk (content or reasoning), then averaged over the streamed calls in this run. The headline interactivity signal: how long a user waits before output begins appearing. Only streaming=True calls contribute -- which the generator restricts to inference -- so this reads 0.0 on runs that never stream.

Example:

program.compile(
    metrics=[
        synalinks.metrics.AvgTimeToFirstToken(),
    ],
)
Source code in synalinks/src/metrics/lm_metrics.py
@synalinks_export("synalinks.metrics.AvgTimeToFirstToken")
class AvgTimeToFirstToken(LMOperationalMetric):
    """Mean time to first token of streamed calls; for UX latency, 0 unstreamed.

    Measured per streamed call as the wall-clock from the provider request
    start to the first non-empty chunk (content or reasoning), then averaged
    over the streamed calls in this run. The headline interactivity signal:
    how long a user waits before output begins appearing. Only `streaming=True`
    calls contribute -- which the generator restricts to inference -- so this
    reads 0.0 on runs that never stream.

    Example:

    ```python
    program.compile(
        metrics=[
            synalinks.metrics.AvgTimeToFirstToken(),
        ],
    )
    ```
    """

    def __init__(self, name="avg_time_to_first_token", description=None):
        super().__init__(name=name, description=description)

    def result(self):
        calls = self._delta("streaming_calls")
        if calls <= 0:
            return 0.0
        return self._delta("streaming_ttft_s") / calls